/*
cAndy Apple Core: An Apple //e emulator engine
    - based almost entirely on AppleWin

Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2008, Tom Charlesworth, Michael Pohoreski, Nick Westgate
Copyright (C) 2009-2010, Garnet Ulrich

cAndy Apple Core is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

cAndy Apple Core is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with cAndy Apple Core; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "StdAfx.h"
#pragma  hdrstop

//----------------------------------------------------------------------------- */

void Snapshot_LoadState(char* filename) {

    CACore_SNAPSHOT* pSS = (CACore_SNAPSHOT*) new char[sizeof(CACore_SNAPSHOT)];

	if(pSS == NULL) {
	    // To do
	    return;
	}

	memset(pSS, 0, sizeof(CACore_SNAPSHOT));

	FILE* file = fopen(filename,"r");
	fread (pSS, 1, sizeof(CACore_SNAPSHOT), file);
	fclose(file);

	if(pSS->Hdr.dwTag != 'Z') {
        //bad tag
	}

	if(pSS->Hdr.dwVersion != MAKE_VERSION(1,0,0,0))	{
	    //bad version
	}

	// Reset all sub-systems
	MemReset();

	if (!IS_APPLE2) {
	    MemResetPaging();
	}

	DiskReset();
	KeybReset();
	VideoResetState();

	//
	// Apple2 uint
	//

	CpuSetSnapshot(&pSS->Apple2Unit.CPU6502);
	JoySetSnapshot(&pSS->Apple2Unit.Joystick);
	KeybSetSnapshot(&pSS->Apple2Unit.Keyboard);
	SpkrSetSnapshot(&pSS->Apple2Unit.Speaker);
	VideoSetSnapshot(&pSS->Apple2Unit.Video);
	MemSetSnapshot(&pSS->Apple2Unit.Memory);
    CACoreSetSnapshot(&pSS->Apple2Unit.CACore);

	// Slot6: Disk][
	DiskSetSnapshot(&pSS->Disk2, 6);
	delete [] pSS;
}

//-----------------------------------------------------------------------------

void Snapshot_SaveState(char* filename) {

	CACore_SNAPSHOT* pSS = (CACore_SNAPSHOT*) new char[sizeof(CACore_SNAPSHOT)];
	if(pSS == NULL) {
		// To do
		return;
	}

	memset(pSS, 0, sizeof(CACore_SNAPSHOT)); //zero out the memory
	pSS->Hdr.dwTag = 'Z';
	pSS->Hdr.dwVersion = MAKE_VERSION(1,0,0,0);
	pSS->Hdr.dwChecksum = 0;	// TO DO

	//
	// Apple2 uint
	//

	pSS->Apple2Unit.UnitHdr.dwLength = sizeof(SS_APPLE2_Unit);
	pSS->Apple2Unit.UnitHdr.dwVersion = MAKE_VERSION(1,0,0,0);
	CpuGetSnapshot(&pSS->Apple2Unit.CPU6502);
	JoyGetSnapshot(&pSS->Apple2Unit.Joystick);
	KeybGetSnapshot(&pSS->Apple2Unit.Keyboard);
	SpkrGetSnapshot(&pSS->Apple2Unit.Speaker);
	VideoGetSnapshot(&pSS->Apple2Unit.Video);
	MemGetSnapshot(&pSS->Apple2Unit.Memory);
    CACoreGetSnapshot(&pSS->Apple2Unit.CACore);

	// Slot6: Disk][
	DiskGetSnapshot(&pSS->Disk2, 6);

    FILE* file = fopen(filename,"w+");

    fwrite (pSS, 1, sizeof(CACore_SNAPSHOT), file);
    fflush(file);
    fclose(file);

	delete [] pSS;
}

